<?php

class Service_Beatport extends Service implements IService
{
    
    static $moduleName = 'Beatport Parser';
    static $author = 'KnKill';
    static $version = '0.90 Beta 2';
    static $site = 'www.beatport.com';
    static $mask = '.*beatport.com\/release\/(.*)\/{1,}[0-9]';
    static $metaFields = array(
        META_LABEL,
        META_GENRES,
        META_RELEASE_DATE,
        META_CATALOG,
        META_REVIEW,
        META_TRACK_BPM
    );
    
    private $content;

    public function __construct($url) {
        $this->content = getPage($url);
        phpQuery::newDocumentHTML($this->content);
    }
    
    
    /**
     * getReleaseArtists
     */
    public function getReleaseArtists() {
        $artists = array();
        $artistSelector = pq('.release-detail > .lastUnit > div.txt-dark-blue')->find('a');
        if(count($artistSelector) == 0) {
            $artist = trim(ucwords(strtolower($artistSelector->text())));
            $artists = array($artist);
        } else {
            foreach($artistSelector as $artist) {
                $artist = trim(pq($artist)->text());
                if(! empty($artist)) {
                    $artists[] = $artist;
                }
            }
        }
        return $artists;
    }
    
    
    /**
     * getReleaseTtitle
     */
    public function getReleaseTitle() {
        $title = pq('.release-detail > .lastUnit h2')->eq(0)->text();
        return trim(ucwords(strtolower($title)));
    }
        
    
    /**
     * getGenres
     */
    public function getGenres() {
        $genres = array();
        foreach(pq('.genreList') as $genre) {
            $genres[] = trim(pq($genre)->find('a')->eq(0)->text());
        }
        return array_unique($genres);
    }
    
    
    /**
     * getLabel
     */
    public function getLabel() {
        $label = pq('table.meta-data')->find('tr')->eq(1)->find('td')->eq(1)->text();
        return trim($label);
    }
    
    
    /**
     * getCatalog
     */
    public function getCatalog() {
        $catalog = pq('table.meta-data')->find('tr')->eq(2)->find('td')->eq(1)->text();
        return trim($catalog);
    }
    
    
    /**
     * getReleaseDate
     */
    public function getReleaseDate() {
        $rd = pq('table.meta-data')->find('tr')->eq(0)->find('td')->eq(1)->text();
        return trim($rd);
    }
    
    
    /**
     * getTracklist
     */
    public function getTracklist() {
        $tracklist = array();
        foreach(pq('.track-grid-content') as $track) {
        
            $pqTrack = pq($track);
        
            $index = (int)trim($pqTrack->find('.playColumn span')->eq(0)->text());
            
            $pqTc = $pqTrack->find('.titleColumn a span');
            
            $title = $pqTc->eq(0)->text();
            $additional = $pqTc->eq(1)->text();
            
            if ($additional != 'Original Mix') {
                $title = $title . ' (' . $additional . ')';
            }
            
            $artists = array();
            foreach($pqTrack->find('.artistList a') as $artist) {
                $artist = pq($artist)->text();
                $artist = trim(ucwords(strtolower($artist)));
                $artists[] = $artist;
            }
            
            $data = $pqTrack->find('.titleColumn')->next('td')->children('span:first')->text();
            preg_match('/([0-9]{1,}:[0-9]{2,}) \/ ([0-9]{1,}) BPM.*/', $data, $data);
            $length = trim($data[1]);
            $bpm = trim($data[2]);
            
            $tracklist[(int)$index] = array(
                'artists' => $artists,
                'title' => $title,
                'length' => $length,
                'bpm' => (int)$bpm
            );

        }
        return $tracklist;
    }
    
    
    /**
     * getReview
     */
    public function getReview() {
        $description = trim(pq('p.description')->eq(0)->text());
        return (empty($description)) ? false : $description;
    }

}